var util = require("../../../utils/util.js"),
{api} = require("../../../utils/api.js"),
    app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        from: {
            name: '',
            institution_id: '',
            institution_name: '',
            mobile: '',
            nickname: '',
            id_type: '中国居民身份证',
            id_number: '',
            birth: '',
            sex: '',
            sex_name: '',
            email: '',
            area: '',
            is_me:0
        },
        region: [],
        agencyShow: false,
        agencyColumns: [],
        agencyList: [],
        certificateShow: false,
        certificateColumns: ['中国居民身份证', '港澳居民往来内地通行证', '港澳居民居住证', '台湾居民来往大陆通行证','台湾居民居住证','护照'],
        sexShow: false,
        sexColumns: ['男', '女'],
        birthdayShow: false,
        pageType:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.patientStortList();
        const {id,is_me,type}=options;
        if(util.regularConfig.isString(id)){
            this.setData({id});
            this.patientDetail();
        }
        if(is_me && is_me == 1){
            this.data.from.is_me=1;
            if(type && type == 1){
                this.data.from.mobile=wx.getStorageSync('userMobile');
            }
            this.setData({from:this.data.from})
        }
        
    },
    async patientStortList() {
        let {
            data
        } = await api.patientStortList();
        let list=data && Array.isArray(data.list)?data.list:[]
        this.setData({
            agencyColumns:list.map(o=>o.name),
            agencyList:list
        })
    },
    async patientDetail() {
        let {
            data
        } = await api.patientDetail({
            patient_id: this.data.id
        });
        console.log('data',data);
        if(data){
            this.data.from.name=data.name;
            this.data.from.institution_id=data.institution_id;
            this.data.from.mobile=data.mobile;
            this.data.from.nickname=data.nickname;
            this.data.from.id_type=data.id_type;
            this.data.from.id_number=data.id_number;
            this.data.from.birth=data.birth;
            this.data.from.sex=data.sex;
            this.data.from.sex_name=data.sex_name;
            this.data.from.email=data.email;
            this.data.from.area=data.area && data.area.indexOf(',')!=-1 && Array.isArray(data.area.split(','))?data.area.split(','):[];
            this.data.agencyList.forEach(item=>{
                if(item.id == data.institution_id){
                    this.data.from.institution_name=item.name;
                }
            })
            this.setData({from:this.data.from})
        }
    },
    onClose() {
        this.setData({
            agencyShow: false,
            certificateShow: false,
            sexShow: false,
            birthdayShow: false,
        })
    },
    bindRegionChange(e) {
        if (Array.isArray(e.detail.value) && e.detail.value.length > 2) {
            let region = e.detail.value;
            this.data.from.area = `${region[0]},${region[1]},${region[2]}`;
            this.setData({
                from: this.data.from,
                region
            })
        }
    },
    agencyClick() {
        this.setData({
            agencyShow: true
        })
    },
    certificateClick() {
        this.setData({
            certificateShow: true
        })
    },
    birthdayClick() {
        this.setData({
            birthdayShow: true
        })
    },
    sexClick() {
        this.setData({
            sexShow: true
        })
    },
    onAgencyChange(e) {
        this.data.from.institution_id = this.data.agencyList[e.detail.index].id;
        this.data.from.institution_name = e.detail.value;
        this.setData({
            from: this.data.from,
            agencyShow:false
        })
    },
    onCertificateChange(e) {
        this.data.from.id_type = e.detail.value;
        this.setData({
            from: this.data.from,
            certificateShow:false
        })
    },
    onSexChange(e) {
        this.data.from.sex = e.detail.value == '男' ? 1 : 2;
        this.data.from.sex_name = e.detail.value;
        this.setData({
            from: this.data.from,
            sexShow:false
        })
    },
    onBirthday(e) {
        this.data.from.birth = e.detail.value;
        this.setData({
            from: this.data.from
        })
    },
    onChange(e) {
        let key = e.currentTarget.dataset.key;
        this.data.from[key] = e.detail;
        this.setData({
            from: this.data.from
        })
    },
    async submit(e) {
        const {name,institution_id,mobile,id_type,id_number,email}=this.data.from;
        if (!name) return wx.showToast({
            title: '用户姓名错误',
            icon: 'none'
        })
        if (!institution_id) return wx.showToast({
            title: '登记门诊未选择',
            icon: 'none'
        })
        if (!util.regularConfig.mobile.test(mobile)) return wx.showToast({
            title: '电话格式错误',
            icon: 'none'
        })
        if (id_type == '中国居民身份证' && id_number && !util.regularConfig.IDcard.test(id_number)) return wx.showToast({
            title: '身份证格式错误',
            icon: 'none'
        })
        if (email && !util.regularConfig.isEmail.test(email)) return wx.showToast({
            title: '邮箱格式错误',
            icon: 'none'
        })
        
        if(this.data.id)this.data.from.patient_id=this.data.id;
        let {
            code
        } = await api[(this.data.id?'patientEdit':'patientAdd')](this.data.from);
        if (code == 1) {
            wx.showToast({
                title: '操作成功',
                icon: 'success'
            })
            setTimeout(() => wx.navigateBack(), 1500)
        }

        // if(this.data.pageType == 1){
        //     wx.showLoading({
        //       title: '提交中...',
        //     })
        //     wx.login({
        //         success: (res) => {
        //           let data = {
        //               code: res.code,
        //               iv: e.detail.iv,
        //               encrypted: e.detail.encryptedData
        //           };
        //           api.getMobile(data)
        //           .then(result => {
        //               let formData=that.data.from;
        //               formData.mobile=result.data.mobile;
        //               return api.patientAdd(formData)
        //           })
        //           .then(response=>{
        //             if (response.code == 1) {
        //                 wx.showToast({
        //                     title: '操作成功',
        //                     icon: 'success'
        //                 })
        //                 setTimeout(() => wx.navigateBack(), 1500)
        //             }
        //           })
        //         },
        //       })
        // }else{
            
        // }
        
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {
        return util.shareEvent(e)
      }
})